//
//  Timer+Extension.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/3.
//

import Foundation

extension Timer: LXSExtensionCompatible {}

public extension LXS where Base: Timer {
    static func scheduledTimer(timeInterval ti: TimeInterval, target aTarget: NSObjectProtocol, selector aSelector: Selector, userInfo aInfo: Any?, repeats yesOrNo: Bool) -> Timer {
        let proxy = WeakTimerProxy.init(target: aTarget, sel: aSelector)
        let timer = Timer.scheduledTimer(timeInterval: ti, target: proxy, selector: aSelector, userInfo:aInfo, repeats: yesOrNo)
        proxy.timer = timer
        return timer
    }
}

public extension Timer {
    static func dc_scheduledTimer(timeInterval ti: TimeInterval, repeats yesOrNo: Bool, block aBlock: @escaping (_ timer: Timer) -> Void) -> Timer {
        Timer.scheduledTimer(timeInterval: ti, target: self, selector: #selector(timerEvent(timer:)), userInfo: aBlock, repeats: yesOrNo)
    }
    
    @objc private static func timerEvent(timer: Timer) {
        if let block = timer.userInfo as? () -> Void {
            block()
        }
    }
}

